package com.xbts.system.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.xbts.common.core.domain.Setting;
import com.xbts.common.enums.SettingEnum;
import com.xbts.common.enums.SmsTemplateEnum;
import com.xbts.common.exception.ServiceException;
import com.xbts.system.domain.dto.SmsDTO;
import com.xbts.system.domain.dto.SmsSetting;
import com.xbts.system.module.sms.ALiYunSmsUtil;
import com.xbts.system.module.sms.HWYSmsUtil;
import com.xbts.system.service.ISmsService;
import com.xbts.system.service.SettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class SmsServiceImpl implements ISmsService {

    @Autowired
    private HWYSmsUtil hwySmsUtil;
    @Autowired
    private ALiYunSmsUtil aLiYunSmsUtil;
    @Autowired
    private SettingService settingService;

    @Override
    public String sendMsg(SmsDTO smsDTO, SmsTemplateEnum smsTemplateEnum) {
        if (smsDTO == null) {
            throw new RuntimeException("短信类型不能为空");
        }

        Setting setting = settingService.get(SettingEnum.SMS_SETTING.name());
        SmsSetting smsSetting = null;
        if (setting != null) {
            smsSetting = JSONObject.parseObject(setting.getSettingValue(), SmsSetting.class);
        }

        int timeOut = 5; // 短信超超时，默认5分钟
        String msg = "";
        switch (Objects.requireNonNull(smsSetting).getSmsSendingType()) {
            case SMS_ALI_SETTING:
//                msg = aLiYunSmsUtil.sendMsg(smsDTO, smsTemplateEnum, timeOut);
                break;
            case SMS_HWY_SETTING:
                msg = hwySmsUtil.sendMsg(smsDTO, smsTemplateEnum, timeOut);
                break;
            default:
                throw new ServiceException("短信类型错误");
        }
        return msg;
    }
}